#hoursNamespaceTenure.py
#Collects hours from files of namespace by startyear
#
#Health of Online Communities


#Currently, we use another method to calculate tenure and offset. We set the start of offset year 0 as the date when the first edit was made. And then we add one year to it to get #the starting point of offset year 1. 
#We also merged the two longest tenure groups together, and ignored the hours spent in 2012 since we only have data up to a certain point in 2012.

#Editor: Guanyu Wang


import os
import collections
import sys
import multiprocessing
import time

def main():
	
	inputDir = sys.argv[1]
	outputDir = sys.argv[2]

	inputFileList = os.listdir(inputDir)

	workers = []
	
	for inputFileName in inputFileList:
		print inputFileName
		
		inputFilePath = inputDir+'/'+inputFileName
		outputFilePath = outputDir+'/'+inputFileName
		
		if os.path.isfile(inputFilePath):

			worker = StatWorker(inputFilePath, outputFilePath)
			workers.append(worker)
			worker.start()

	for worker in workers:
		worker.join()

class StatWorker(multiprocessing.Process):

	def __init__(self, inputFilePath, outputFilePath):
		multiprocessing.Process.__init__(self)
		self.inputFilePath = inputFilePath
		self.outputFilePath = outputFilePath

	def run(self):
		stats(self.inputFilePath, self.outputFilePath)
		print "Finished statistics for " + self.inputFilePath + " in " + self.outputFilePath + "\n"
			
		
		
#prints out the facts
def stats(inputFilePath, outputFilePath):
	inputFileHandle = open(inputFilePath,"r")
	outputFileHandle = open(outputFilePath, 'a+')

	statDict = collections.defaultdict(float)
	outputFileHandle.write("Stats for " + inputFilePath)
	
        fmt = "%Y-%m-%d"       

	for line in inputFileHandle:
		pieces = line.split('\t')
                hours = float(pieces[1])/3600  #seconds -> hours

                startyear = pieces[7]                
                offset = int(pieces[8])
                tenure = int(pieces[10].replace('\n','')) 

                if int(startyear)+tenure == 2012 and int(startyear)+offset == 2012:   #ignores hours spent in 2012
                   hours = 0

                if int(startyear)==2012:
                   startyear = 2011
                   hours = 0   #ignores hours spent in 2012
                if int(startyear)+tenure == 2012:
                   tenure -=1
                if int(startyear)+offset == 2012:
                   offset -=1             	
                
		statDict[str(startyear)+str(offset)+str(tenure)] += hours #hours by start year, offset year, and tenure 
		statDict["TOTAL"] += hours #total hours done in namespace by user who started in start year 
	
	for y in sorted(statDict.keys()):
		outputFileHandle.write(str(y) + ' ' + str(statDict[y]) + '\n') #prints "start year,offset year,tenure" followed by # of hours
	
	inputFileHandle.close()
	outputFileHandle.close()

main()
